package com.hqyj.web.servlet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hqyj.pojo.Book;
import com.hqyj.service.UserService;
import com.hqyj.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author cz
 * 2021/8/12 15:42
 */
@WebServlet("/managerSystem.do")
public class ManagerSystemServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String flag = req.getParameter("flag");

        //业务操作
        if ("addBook".equals(flag)){
            this.addBook(req,resp);
        }else if ("preUpdateBook".equals(flag)){
            this.preUpdateBook(req,resp);
        }else if ("updateBook".equals(flag)){
            this.updateBook(req,resp);
        }else if("deleteBook".equals(flag)){
            this.deleteBook(req,resp);
        }else if ("preDeleteBook".equals(flag)){
            this.preDeleteBook(req,resp);
        }else if ("backmanagermain".equals(flag)){
            this.findBookAll(req,resp);
        }else if ("init".equals(flag)){
            this.findBookAll(req, resp);
        }
    }

    //管理主界面
    private void findBookAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        List<Book> list = userService.findBookAll();
        JSONArray jsonArray = (JSONArray)JSONArray.toJSON(list);
        PrintWriter pw = resp.getWriter();
        pw.println(jsonArray);
    }

    //查询到未被借阅的图书
    private void preDeleteBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> list = userService.findUnborrowBooks();
        req.setAttribute("list",list);
        req.getRequestDispatcher("/dropBook.jsp").forward(req,resp);
    }

    //删除图书
    private void deleteBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bookId = req.getParameter("bookId");
        userService.dropBook(Integer.parseInt(bookId));

        //再次查询所有未借阅图书
        List<Book> list = userService.findUnborrowBooks();
        req.setAttribute("list",list);
        req.getRequestDispatcher("/dropBook.jsp").forward(req,resp);
    }

    //更新用户
    private void updateBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Book book = new Book();
        book.setBookId(Integer.parseInt(req.getParameter("bookId")));
        book.setBookName(req.getParameter("bookname"));
        book.setAuthor(req.getParameter("author"));
        book.setPublisher(req.getParameter("publisher"));
        book.setPubDate(req.getParameter("pubdate"));
        userService.modifyBook(book);

        //再次查询一遍所有图书信息
        List<Book> list = userService.findBookAll();
        req.setAttribute("list",list);
        req.getRequestDispatcher("/managermain.jsp").forward(req,resp);
    }

    //图书预更新
    private void preUpdateBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bookId = req.getParameter("bookId");
        Book book = userService.preModifyBook(Integer.parseInt(bookId));
        req.setAttribute("book",book);
        req.getRequestDispatcher("/updateBook.jsp").forward(req,resp);
    }

    //新增图书
    private void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Book book = new Book();

        book.setBookName(req.getParameter("bookname"));
        book.setAuthor(req.getParameter("author"));
        book.setPublisher(req.getParameter("publisher"));
        book.setPubDate(req.getParameter("pubdate"));
        userService.addBook(book);
        resp.getWriter().println("<script>alert('新增成功')</script>");
        resp.setHeader("Refresh","0;url='/managermain.jsp'");
    }
}
